<?php

require_once 'Entity.php';

/**
 * Użytkownik.
 *
 * @author Rafał Cupał
 * @author Przemysław Furmańczyk
 */
class User extends Entity {
    protected $email = null;
    protected $name = null;
    protected $location = null;
    protected $telNumber = null;
    protected $newPassword = null;
    
    public function __construct($dataBase) {
        parent::__construct($dataBase, 'users');
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function setName($newName) {
        $this->name = $newName;
    }
    
    public function getLocation() {
        return $this->location;
    }
    
    public function setLocation($newLocation) {
        $this->location = $newLocation;
    }
    
    public function getTelNumber() {
        return $this->telNumber;
    }
    
    public function setTelNumber($newTelNumber) {
        $this->telNumber = $newTelNumber;
    }
    
    public function getEmail() {
        return $this->email;
    }
    
    public function setEmail($newEmail) {
        $this->email = $newEmail;
    }
    
    public function changePassword($newPassword) {
        $this->newPassword = $newPassword;
    }
    
    public function load($rowData) {
        $this->id = $rowData['id'];
        $this->getRowData($rowData, $this->name, 'name');
        $this->getRowData($rowData, $this->email, 'email');
        $this->getRowData($rowData, $this->telNumber, 'telNumber');
        $this->getRowData($rowData, $this->location, 'location');
        $this->getRowData($rowData, $this->newPassword, 'password');
        $this->newPassword = null;
    }
    
    public function save() {
        $rowData = array();
        
        $this->setRowData($rowData, $this->name, 'name');
        $this->setRowData($rowData, $this->email, 'email');
        $this->setRowData($rowData, $this->telNumber, 'telNumber');
        $this->setRowData($rowData, $this->location, 'location');
        $this->setRowData($rowData, $this->newPassword, 'password');
        
        if ($this->id > 0) {
            $this->dataBase->update($this->tableName, $rowData, 
                    'id = ' . $this->id);
        }
        else {
            $this->dataBase->insert($this->tableName, $rowData);
            $this->id = $this->dataBase->lastInsertId();
        }
    }
}
